//
//  GHMerchnFormViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/23.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHMerchnFormViewController.h"
#import "GHMerchanFormTableViewCell.h"
#import "GHMerchanModel.h"
#import "GHFormDetailViewController.h"
#import "GHFormMakeViewController.h"
@interface GHMerchnFormViewController ()
@property(nonatomic, strong) NSMutableArray <GHMerchanModel*>                   *dataSouce;
@property(nonatomic, strong) GHNoResultView                                         *noResultView;
@property(nonatomic, strong) STLabel                     *titleLable;
@end

@implementation GHMerchnFormViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报单中心";
    [self configSubView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 40)];
    header.backgroundColor = [UIColor whiteColor];
    self.titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 0, 200, 40)
                                                     text:@"找到相关报单0个"
                                                textColor:GH_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:self.titleLable];
    __weak typeof(self) weakSelf =  self;
    STButton * addButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 30)
                                                     title:@"立即报单"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:14
                                              cornerRadius:5
                                           backgroundColor:GH_YellowBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    addButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    addButton.centerY = 20;
    addButton.right = UIScreenWidth - 10;
    [addButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddFormButton];
    }];
    [header addSubview:addButton];
    
    self.tableView.tableHeaderView = header;
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendMyformsRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    //无数据view
    self.noResultView = [[GHNoResultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 350) title:@"暂无报单" buttonTitle:@"没有报单过哦" buttonHandle:^(NSString *titleString) {
        
    }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GHMerchanFormTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    GHMerchanFormTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[GHMerchanFormTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.section];
    __weak typeof(self) weakSelf =  self;
    [cell.moreButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedMoreFormButtonWithIndexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
//立即报单
- (void)onSelectedAddFormButton{
    [self.navigationController pushViewController:[GHFormMakeViewController new] animated:YES];
}
- (void)onSelectedMoreFormButtonWithIndexPath:(NSIndexPath*)indexPath{
    GHMerchanModel * model = self.dataSouce[indexPath.section];
    GHFormDetailViewController * vc = [GHFormDetailViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendMyformsRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/get_declaration"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 if ([dataDic.allKeys containsObject:@"declaration_data"]) {
                                                     NSArray * declaration_dataArray = dataDic[@"declaration_data"];
                                                     for (NSDictionary * modelDic in declaration_dataArray) {
                                                         GHMerchanModel * model = [GHMerchanModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 self.titleLable.text= [NSString stringWithFormat:@"找到相关订单%ld个",self.dataSouce.count];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 self.titleLable.text= [NSString stringWithFormat:@"找到相关订单%ld个",self.dataSouce.count];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
